package com.rohoo.user.action;

import java.net.URLDecoder;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.codec.digest.DigestUtils;

import com.rohoo.framework.struts.action.BaseAction;
import com.rohoo.user.bo.Register;
import com.rohoo.user.dto.RegisterDTO;
import com.rohoo.user.service.RegisterService;

public class RegisterAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private RegisterService registerService;
	
	private int user_id;
	private String useridcard;
	private String username;
	private String userpwd;
	private String usersex;
	private String userbirth;
	private String usertel;
	private String usermobile;
	private String useraddress;
	private String usermail;
	private String usercomp;
	private int old_userid;
	//新密码
	private String newpwd;
	
	public String toadd(){
		return SUCCESS;
	}
	
	public String add() throws Exception{
		Register register = new Register();
		boolean result = false;
		username=URLDecoder.decode(username, "utf-8");
		userpwd=URLDecoder.decode(userpwd, "utf-8");
		usersex=URLDecoder.decode(usersex, "utf-8");
		useridcard=URLDecoder.decode(useridcard, "utf-8");
		usertel=URLDecoder.decode(usertel, "utf-8");
		usermobile=URLDecoder.decode(usermobile, "utf-8");
		useraddress=URLDecoder.decode(useraddress, "utf-8");
		usermail=URLDecoder.decode(usermail, "utf-8");
		usercomp=URLDecoder.decode(usercomp, "utf-8");
		BeanUtils.copyProperties(register,this);
		old_userid = registerService.getUserId(useridcard);
		if(old_userid==0){
			if(!"".equals(username)&&!"".equals(userpwd)&&!"".equals(usersex)&&!"".equals(userbirth)
					&&!"".equals(usertel)&&!"".equals(usermobile)
					&&!"".equals(useraddress)&&!"".equals(usermail)&&!"".equals(usercomp)){
				register.setUser_id(registerService.selUserId());
				result=registerService.add(register);
			}
			if(result == true){
				return SUCCESS;
			}
			else{
				return "error2";
			}
		}
		else {
			return "error1";
		}
	}
	
	public String toNext() throws Exception {
		Register user = registerService.getUser(user_id);
		BeanUtils.copyProperties(this, user);
		return SUCCESS;

	}
	
	public String doEditUser() throws Exception {
		RegisterDTO dto = new RegisterDTO();
		username=URLDecoder.decode(username, "utf-8");
		if(!"".equals(newpwd)&&newpwd!=null){
			newpwd=URLDecoder.decode(newpwd, "utf-8");
			newpwd = DigestUtils.md5Hex(newpwd);
		}
		else{
			newpwd = userpwd;
		}
		usersex=URLDecoder.decode(usersex, "utf-8");
		usertel=URLDecoder.decode(usertel, "utf-8");
		usermobile=URLDecoder.decode(usermobile, "utf-8");
		useraddress=URLDecoder.decode(useraddress, "utf-8");
		usermail=URLDecoder.decode(usermail, "utf-8");
		usercomp=URLDecoder.decode(usercomp, "utf-8");
		BeanUtils.copyProperties(dto, this);
		if(!"".equals(username)&&!"".equals(usersex)&&!"".equals(userbirth)
				&&!"".equals(usertel)&&!"".equals(usermobile)
				&&!"".equals(useraddress)&&!"".equals(usermail)&&!"".equals(usercomp)){
			registerService.updateUser(dto);
		}
		return SUCCESS;
	}
	
	public RegisterService getRegisterService() {
		return registerService;
	}
	public void setRegisterService(RegisterService registerService) {
		this.registerService = registerService;
	}
	public int getUser_id() {
		return user_id;
	}
	public void setUser_id(int userId) {
		user_id = userId;
	}
	public String getUseridcard() {
		return useridcard;
	}
	public void setUseridcard(String useridcard) {
		this.useridcard = useridcard;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUserpwd() {
		return userpwd;
	}
	public void setUserpwd(String userpwd) {
		this.userpwd = userpwd;
	}
	public String getUsersex() {
		return usersex;
	}
	public void setUsersex(String usersex) {
		this.usersex = usersex;
	}
	public String getUserbirth() {
		return userbirth;
	}
	public void setUserbirth(String userbirth) {
		this.userbirth = userbirth;
	}
	public String getUsertel() {
		return usertel;
	}
	public void setUsertel(String usertel) {
		this.usertel = usertel;
	}
	public String getUsermobile() {
		return usermobile;
	}
	public void setUsermobile(String usermobile) {
		this.usermobile = usermobile;
	}
	public String getUseraddress() {
		return useraddress;
	}
	public void setUseraddress(String useraddress) {
		this.useraddress = useraddress;
	}
	public String getUsermail() {
		return usermail;
	}
	public void setUsermail(String usermail) {
		this.usermail = usermail;
	}
	public String getUsercomp() {
		return usercomp;
	}
	public void setUsercomp(String usercomp) {
		this.usercomp = usercomp;
	}
	public int getOld_userid() {
		return old_userid;
	}
	public void setOld_userid(int oldUserid) {
		old_userid = oldUserid;
	}
	public String getNewpwd() {
		return newpwd;
	}
	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}
}
